import './commonDocument.scss';
import PaBaseModel from '../../core/model/PaBaseModel.js';

function commonDocumentCtrl($scope, $http, ngDialog, paFetchService) {
  'ngInject';
  let _this = this;
  $scope.isUploading = false;
  $scope.commonType = $state.params.commonType;

  // 分页
  $scope.queryData = new PaBaseModel({
    documentType: $scope.id,
    documentName: $scope.commonType,
    recordno: '',
    archiveName: '',
  });
  $scope.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  };

  $scope.reset = () => {
    $scope.queryData = {};
  }

  // 分页查询档案
  $scope.initViewModel = () => {
   $http.post(`${SERVER_CONTEXT}/api/safesite/officeworkdata/selectByCondition`,
       angular.extend($scope.queryData, $scope.paginationData)).
   then((resp) => {
     $scope.documentList = resp.data.data;
     $scope.paginationData.total = resp.data.total;
   });
  };
  $scope.initViewModel();
  
  // 页码变化时调用方法
  $scope.pageChange = () => {
    $scope.initViewModel();
  };

  // 获得标段数据
  paFetchService
    .getSectionList()
    .then((resp) => {
      this.sectionList = resp.data;
      $scope.sectionList = resp.data;
    });

  // 上传档案信息
  $scope.newData = () => {
    ngDialog.open({
      template: 'documentItemMgt',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'officeDataAddCtrl',
      scope: $scope,
      controller: function cfgAddCtrl($http , $scope) {
        'ngInject';
        this.category = {
          recordno: '',
          archiveName: '',
          completionTime: '',
          markSection: '',
          organization: '',
          uploader: '',
          relatedDesc:'',
          fileBeanList: [],
          fileIds: [], // 存放上传文件返回token
        };

        // this.sectionList = _this.sectionList;
        // 获得标段数据
        paFetchService
          .getSectionList()
          .then((resp) => {
            this.sectionList = resp.data;
          });

        this.submit = (param) => {
          if ($scope.isUploading) {
            layer.alert('文件正在上传中');
          }
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          this.category.documentType = $scope.id;
          this.category.documentName = $scope.commonType;
          
          let fileIdArray = this.category.fileBeanList.map(item => item.id);
          this.category.fileIds = fileIdArray.join(',');
          let reqData = this.category;
          $http({
            method: 'POST',
            url: `${SERVER_CONTEXT}/api/safesite/officeworkdata/saveRecord`,
            data: reqData,
          }).then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $scope.initViewModel();
              $scope.closeThisDialog();
            } else {
              layer.alert(resp.data.message);
              $scope.closeThisDialog();
            }
          });
        }
      },
    });
  };
  // 查看档案信息
  $scope.getArchiveDetail = (id) => {
    $scope.currentRecordId = id;
    ngDialog.open({
      template: 'archiveDetail',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controller: function($scope) {
        'ngInject';

        // 获得文档详情信息
        let str = `${SERVER_CONTEXT}/api/safesite/officeworkdata/selectByPrimaryKey/`+$scope.currentRecordId;
        $http.get(str).
          then((resp) => {
            $scope.archiveDetail = resp.data;
          });

        // 获得文件下载链接
        $scope.getDownloadSrc = (file) => {
          return `${SERVER_IP}${SERVER_CONTEXT}/api/safesite/officeworkdata/download/${file.id}`;
        }

        // 进入文件预览页面
        $scope.preview = (archiveDetail) => {
          let url = $state.href('documentPreview', {
            id: archiveDetail.id,
          });
          window.open(url,'_blank');
        }
      }
    });
  }
  // 编辑档案信息
  $scope.editeditDocument = {};
  $scope.editData = (id) => {
    $scope.currentRecordId = id;
    ngDialog.open({
      template: 'editDocumentItemMgt',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controller: function($scope, paFetchService) {
        'ngInject';

        $scope.sectionList = _this.sectionList;

        let str = `${SERVER_CONTEXT}/api/safesite/officeworkdata/selectByPrimaryKey/`+$scope.currentRecordId;
        $http.get(str).
        then((resp) => {
          $scope.editeditDocument = resp.data;
        });

        // 获得标段数据
        paFetchService
          .getSectionList()
          .then((resp) => {
            this.sectionList = resp.data;
          });
        $scope.submit = () => {
          $scope.fileIds = [];
          $scope.fileIds = $scope.editeditDocument.fileBeanList.map(item => item.id);
          $scope.editeditDocument.fileIds = $scope.fileIds.join(',');
          $http({
            method: 'PUT',
            url: `${SERVER_CONTEXT}/api/safesite/officeworkdata/updateRecord`,
            data: $scope.editeditDocument,
          }).then((resp) => {
            if (resp.data.success) {
              $scope.initViewModel();
              layer.msg(resp.data.message)
              $scope.closeThisDialog();
            } else {
              layer.alert(resp.data.message);
              $scope.closeThisDialog();
            }
          });
        }
      }
    });
  }
  // 删除信息
  $scope.deleteInformation = (id) => {
    $scope.modalTitle = '删除信息';
    $scope.modalContent = '确认要删除此信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      let reqData = [];
      reqData.push(id);
      $http({
        method: 'DELETE',
        url: `${SERVER_CONTEXT}/api/safesite/officeworkdata/delete`,
        params: {
          recordnos: reqData
        },
      }).
      then((data) => {
        $scope.initViewModel();
      });
    }, (reason) => {});
  };

  // 升序、降序
  $scope.up = (index) => {        
    $scope.queryData.order = index;
    $scope.initViewModel();
  }
}

export default commonDocumentCtrl;